@ECHO OFF

REM **********************************************************************
REM Convert Hex file to Binary file and merge to target
REM 
REM Double click this batch file or use command as below:
REM
REM     post.bat [usbboot]
REM
REM such as:
REM     post.bat
REM     post.bat ub
REM 
REM                                                                By Phil
REM **********************************************************************

REM **********************************************************************
REM Environment configure
REM **********************************************************************

set WORK=.\
set OBJ=obj
set KEIL=C:\Keil\C51\BIN
set USBBOOT=C:\Progra~1\Sunplus\UsbBoot
set TOOL=exe
set DOWNLOAD=download
set RESOURCE=resource
set SOURCE=SP1K
set MIDDLE=fw.bin
set TARGET=1528.bin
set MAP=.\lst\%SOURCE%.map

REM **********************************************************************
REM Check code/data overlap
REM **********************************************************************

findstr /n /c:"*** OVERLAP ***" %MAP%
if %errorlevel% equ 0 (
	goto ERR_OVERLAP
)

REM **********************************************************************
REM IDE Environment check
REM **********************************************************************

if not exist %KEIL%\ohx51.exe (
	goto ERR_IDE_NOT_FOUND
)

REM **********************************************************************
REM Convert to Binary File
REM **********************************************************************

%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B0:0 - B0:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H00) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B1:0 - B1:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H01) OFFSET (-0x10000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B2:0 - B2:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H02) OFFSET (-0x20000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B3:0 - B3:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H03) OFFSET (-0x30000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B4:0 - B4:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H04) OFFSET (-0x40000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B5:0 - B5:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H05) OFFSET (-0x50000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B6:0 - B6:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H06) OFFSET (-0x60000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B7:0 - B7:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H07) OFFSET (-0x70000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B8:0 - B8:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H08) OFFSET (-0x80000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B9:0 - B9:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H09) OFFSET (-0x90000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B10:0 - B10:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H10) OFFSET (-0xA0000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B11:0 - B11:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H11) OFFSET (-0xB0000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B12:0 - B12:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H12) OFFSET (-0xC0000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B13:0 - B13:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H13) OFFSET (-0xD0000) >NUL
%KEIL%\OHX51 %OBJ%\%SOURCE% RANGE (B14:0 - B14:0xFFFF) HEXFILE (%OBJ%\%SOURCE%.H14) OFFSET (-0xE0000) >NUL

%TOOL%\HEX2BIN -e B00 %OBJ%\%SOURCE%.H00 >NUL
%TOOL%\HEX2BIN -e B01 %OBJ%\%SOURCE%.H01 >NUL
%TOOL%\HEX2BIN -e B02 %OBJ%\%SOURCE%.H02 >NUL
%TOOL%\HEX2BIN -e B03 %OBJ%\%SOURCE%.H03 >NUL
%TOOL%\HEX2BIN -e B04 %OBJ%\%SOURCE%.H04 >NUL
%TOOL%\HEX2BIN -e B05 %OBJ%\%SOURCE%.H05 >NUL
%TOOL%\HEX2BIN -e B06 %OBJ%\%SOURCE%.H06 >NUL
%TOOL%\HEX2BIN -e B07 %OBJ%\%SOURCE%.H07 >NUL
%TOOL%\HEX2BIN -e B08 %OBJ%\%SOURCE%.H08 >NUL
%TOOL%\HEX2BIN -e B09 %OBJ%\%SOURCE%.H09 >NUL
%TOOL%\HEX2BIN -e B10 %OBJ%\%SOURCE%.H10 >NUL
%TOOL%\HEX2BIN -e B11 %OBJ%\%SOURCE%.H11 >NUL
%TOOL%\HEX2BIN -e B12 %OBJ%\%SOURCE%.H12 >NUL
%TOOL%\HEX2BIN -e B13 %OBJ%\%SOURCE%.H13 >NUL
%TOOL%\HEX2BIN -e B14 %OBJ%\%SOURCE%.H14 >NUL

%TOOL%\BANKLINK 15 %OBJ%\%SOURCE% %OBJ%\%SOURCE%.BIN >NUL

REM **********************************************************************
REM Merge to target file
REM **********************************************************************

COPY %OBJ%\%SOURCE%.BIN %DOWNLOAD%\%MIDDLE% >NUL

pushd %DOWNLOAD%
RESMERGER %TARGET%
popd

REM **********************************************************************
REM USB Boot
REM **********************************************************************

if "%1" == "ub" (
	%USBBOOT%\usbboot.exe 0 %WORK%\%DOWNLOAD%\fw.bin
)

REM %TOOL%\down.exe %WORK%\%DOWNLOAD%\fw.bin
REM %TOOL%\EP4.exe %WORK%\%DOWNLOAD%\fw.bin

REM **********************************************************************
REM Finish Process
REM **********************************************************************

exit

REM **********************************************************************
REM Error handle
REM **********************************************************************

:ERR_OVERLAP
ECHO -------------------------------------------------------
ECHO  Build target fail!
ECHO  Code/Data overlap, please check %MAP%
ECHO -------------------------------------------------------
pause
exit

:ERR_IDE_NOT_FOUND
ECHO -------------------------------------------------------
ECHO  Build target fail!
ECHO  Couldn't find Keil IDE tools in %KEIL%\
ECHO  Please configure IDE environment in ".\post.bat"
ECHO -------------------------------------------------------
pause
exit

:end
